//关闭loading动画
const requestHideLoading = (timeId) => {
	// console.log('-0-----requestHideLoading',timeId)
	uni.hideLoading()
	clearTimeout(timeId)

}

/**
 * 请求拦截
 * @param {Object} http
 */
const requestInterceptors = (vm) => {
	uni.$uv.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
			// 初始化请求拦截器时，会执行此方法，此时data为undefined，赋予默认{}
			config.data = config.data || {}
			// 可以在此通过vm引用vuex中的变量，具体值在vm.$store.state中
			// console.log(vm.$store.state);
			const storeState = vm.config.globalProperties.$store.state
			// console.log('？？？？？？？？？？？', vm.config.globalProperties.$store.state, vm.$store);
			if (config.url.indexOf("http") === 0) { //第三方

			} else {

				// config.header['ProxyChannel'] = 'H5'
				// config.header['App_id'] = "com.happy.work"
				// config.header['App_name'] = "Instant Mulla"
				// config.header['App_version'] = "1"
				// config.header['appsflyer_id'] = "Instant Mulla"
				// config.header['app_instance_id'] = "Instant Mulla"
				// config.header['Authorization'] = "Bearer "+storeState.vuex_token
				// config.header['Accept-Language'] = storeState.vuex_country
				// config.header['Content-Type'] = "application/json;charset=UTF-8"
				// config.header['ProxyChannel'] = "H5"
				config.header['App_id'] = "com.happy.work"
				config.header['App_name'] = encodeURI("Prêt Sécurisé")
				config.header['App_version'] = "1"
				config.header['appsflyer_id'] =  encodeURI("Prêt Sécurisé")
				config.header['app_instance_id'] =  encodeURI("Prêt Sécurisé")
				config.header['Authorization'] = "Bearer "+storeState.vuex_token
				config.header['Accept-Language'] = "en"
				config.header['Content-Type'] = "application/json"
				config.header['ProxyChannel'] = "H5"
				if(config.url==='/ddzcaptchaddzaz/ddzjkgetCaptcha'){
					config.responseType = 'arraybuffer'
					// responseType:url=='/ddzcaptchaddzaz/dmqzgetCaptcha'?'arraybuffer':''
				}
				
			}
			return config
		}, (config) => // 可使用async await 做异步操作
		Promise.reject(config))
}

/**
 * 响应拦截
 * @param {Object} http 
 */
const responseInterceptors = (vm) => {
	uni.$uv.http.interceptors.response.use((response) => {
		/* 对响应成功做点什么 可使用async await 做异步操作*/
		const custom = response.config?.custom

		// console.log('---response', response)
		// console.log('---2', custom)

		//关闭loading动画
		if (!custom.ignoreLoaing) {
			requestHideLoading(custom.loadingTimer)
		}

		if (custom && custom.return) {
			return response.data
		}

		//请求完成
		custom?.completeFnc(response)

		if (response.statusCode != 200) {
			uni.$uv.toast("err:" + response.statusCode)
		}
		return response.data;
		const {code, msg, data} = response.data || {}
		// if (custom.showResData) {
		// 	console.log('--http-response', data, response)
		// }
		// console.log('---Object', code, msg, data)
		if (code === -1) {
			uni.$uv.toast(msg);
			uni.$emit('loginEvent')
			return Promise.reject(response.data)
		} else if (code == -2) { //账号被禁用
			uni.$uv.toast(msg);
			vm.$store.dispatch('handleLogout')
			return
		} else if (code !== 1) {
			if (custom.errShowToast) {
				uni.$uv.toast(msg);
			}
			return Promise.reject(response.data)
		} else {
			return response.data;
		}
	}, (response) => {
		/*  对响应错误做点什么 （statusCode !== 200）*/
		const custom = response.config?.custom
		
		
		//关闭loading动画
		requestHideLoading(custom.loadingTimer)
		
		//请求完成
		custom?.completeFnc(response)
		
		if (response.errMsg === "request:fail timeout") {
			uni.hideLoading();
			uni.$uv.toast("网络请求超时")
		}
		return Promise.reject(response)
	})
}


export {
	requestInterceptors,
	responseInterceptors
}